<template>
  <div>
    <div class="header">
      <img src="@/assets/images/return.png" class="header_img" @click="back"/>
      <div class="title">工作经历</div>
      <div class="save" @click="saveBack">保存</div>
    </div>
    <div class="base_message">
      <div class="inputGroup">
        <div class="input_title">公司</div>
        <input type="text" placeholder="请输入公司名称" class="input_value" v-model="data.company"/>
      </div>
      <div class="inputGroup">
        <div class="input_title">职位</div>
        <input type="text" placeholder="请输入职位名称" class="input_value" v-model="data.position"/>
      </div>
      <div class="inputGroup" @click="open('timePicker',1)">
        <div class="input_title">开始时间</div>
        <div class="input_value">{{data.startTime | formatDate('YYYY-MM-DD')}}</div>
      </div>
      <div class="inputGroup" @click="open('timePicker',2)">
        <div class="input_title">结束时间</div>
        <div class="input_value">{{data.endTime | formatDate('YYYY-MM-DD')}}</div>
      </div>
    </div>
    <div class="base_message">
      <div class="inputGroup" @click="setSelect">
        <div class="input_title flex">优先展示本项工作经历</div>
        <div class="checkbox"><img :class="['selected_img',isAdvance?'selected_show': '']"
                                   src="@/assets/images/selected.png" class="selected_img"/></div>
      </div>
    </div>
    <div class="base_message">
      <div class="work_describe">
        <div class="work_title">经历描述</div>
        <div class="work_text"><text-area classProp="work_p" placeholder="请从主要工作内容、成就等方面，描述你的工作经历，字数不少于60字" @sendVal="setValue"></text-area></div>
      </div>
    </div>
    <div class="base_message">
      <div class="workProve">
        <div class="prove_title">工作证明</div>
        <div class="prove_picture">
          <div class="picImage" v-for="(file, index) in files" :key="index"><div class="pic_I" v-html="file"></div><div class="deleteIcon" @click="deleteFile(index)"></div></div>
          <div ref="picBox" class="pic" @click="takePhoto"><img src="@/assets/images/proveAdd.png" class="prove-add"/></div>
        </div>
        <div class="prove_explain">工作证明可以是个人名片、在职证明、工牌、工作邮箱截图、钉钉截图等，必须清晰可认、包含本人信息</div>
      </div>
    </div>
    <mt-datetime-picker
      ref="timePicker"
      type="date"
      v-model="currentDate"
      :endDate="endDate"
      :startDate="startDate"
      @confirm="handleChange">
    </mt-datetime-picker>
    <input type="file" ref="fileUpload" @change="fileSelected" style="display: none"/>
  </div>
</template>

<script>
  import TextArea from './TextArea'
  var Moment = require('moment')
  import { Toast} from 'mint-ui'
  export default {
    name: "WorkExperiment",
    components: {
      TextArea
    },
    data() {
      return {
        endDate:new Date(),
        currentDate: new Date(),
        startDate: new Date(1970,1,1),
        isAdvance: false,
        files:[],
        filesImages:[],
        data:{
          company:null,// 公司
          position: null, // 职位
          startTime: null, // 开始时间
          endTime: null, // 结束时间
          experienceDesc: null, // 工作经历描述
          workCertificate: null, // 工作证明
          showFirst: null, // 优先展示
        }
      }
    },
    methods: {
      deleteFile(index){
        this.files.splice(index,1)
        this.filesImages(index,1)
      },
      fileSelected(){
        var oFile = this.$refs['fileUpload'].files[0]
        var rFilter = /^(image\/bmp|image\/gif|image\/jpeg|image\/png|image\/tiff)$/i
        //如果文件不是图片提示不是图片
        if(!rFilter.test(oFile.type)){
          Toast({
            message: '文件格式必须为图片',
            position: 'middle',
            duration: 1000
          });
        }else{
          var reader = new FileReader(), img = new Image(), src = ''

          reader.readAsDataURL(oFile)
          var self = this
          reader.onload=function(e){
            img.src=e.target.result
            src=e.target.result
          }
          img.onload = function() {
            // 图片原始尺寸
            var originWidth = this.width
            var originHeight = this.height

            // 最大尺寸限制，可通过设置宽高来实现图片压缩程度
            var maxWidth = $(self.$refs['picBox']).width(), maxHeight = $(self.$refs['picBox']).height()
            // 目标尺寸
            var targetWidth = originWidth, targetHeight = originHeight
            // 图片尺寸超过220x180的限制
            if (originWidth > maxWidth || originHeight > maxHeight) {
              if (originWidth / originHeight > maxWidth / maxHeight) {
                // 更宽，按照宽度限定尺寸
                targetWidth = 100 + '%';
                targetHeight = (originHeight * maxWidth) / (originWidth * maxHeight) * 100 + '%';
              } else {
                targetHeight = 100 + "%";
                targetWidth =  (originWidth * maxHeight) / (originHeight * maxWidth) * 100 + '%';
              }
              self.filesImages.push(oFile)
              self.files.push(`<img src="${src}" style="width: ${targetWidth}; height: ${targetHeight}"/>`)
            }else{
              self.filesImages.push(oFile)
              self.files.push(`<img src="${src}" style="width: ${targetWidth}px; height: ${targetHeight}px"/>`)
            }
          }
        }
      },
      takePhoto() {
        $(this.$refs['fileUpload']).click()
      },
      open(picker,type) {
        this.pickerType = type;
        this.$refs[picker].open();
      },
      back() {
        this.$router.back()
      },
      setSelect() {
        this.isAdvance = !this.isAdvance
        if(this.isAdvance){
          this.data.showFirst = 1
        }else{
          this.data.showFirst = 0
        }
      },
      setValue(text){
        this.data.experienceDesc = text
      },
      handleChange(value) {
        switch (this.pickerType) {
          case 1:
            if( value.getTime() > new Date().getTime()){
              Toast({
                message: '开始时间不能大于当前时间',
                position: 'middle',
                duration: 1000
              });
            }else if(this.data.endTime && value.getTime() > this.data.endTime.getTime() ){
              Toast({
                message: '开始时间不能大于结束时间',
                position: 'middle',
                duration: 1000
              });
            }else{
              this.data.startTime = value
            }
            break
          case 2:
            if( value.getTime() > new Date().getTime()){
              Toast({
                message: '结束时间不能大于当前时间',
                position: 'middle',
                duration: 1000
              });
            }else if(this.data.startTime && value.getTime() < this.data.startTime.getTime() ){
              Toast({
                message: '结束时间不能小于开始时间',
                position: 'middle',
                duration: 1000
              });
            }else{
              this.data.endTime = value
            }
            break
        }
      },
      saveBack(){
        if(!this.data.company){
          Toast({
            message: '请输入公司名称',
            position: 'middle',
            duration: 1000
          });
        }else if(!this.data.position){
          Toast({
            message: '请输入职位',
            position: 'middle',
            duration: 1000
          });
        }else if(!this.data.startTime){
          Toast({
            message: '请输入开始时间',
            position: 'middle',
            duration: 1000
          });
        }else if(!this.data.endTime){
          Toast({
            message: '请输入结束时间',
            position: 'middle',
            duration: 1000
          });
        }else if(!this.data.experienceDesc){
          Toast({
            message: '请输入您的工作经历',
            position: 'middle',
            duration: 1000
          });
        } else{
          Moment.locale('zh-cn');
          this.data.startTime = Moment(this.data.startTime.toString()).format('YYYY-MM-DD')
          this.data.endTime =  Moment(this.data.endTime.toString()).format('YYYY-MM-DD')
          if(this.filesImages.length !== 0){
            this.data.file = this.filesImages[0]
          }
          this.$store.dispatch('action_addWorkExperiment', this.data)
          this.$router.back()
        }
      }
    },
    mounted() {
    }
  }
</script>

<style scoped>
  .header {
    box-sizing: border-box;
    position: relative;
    display: flex;
    height: 90px;
    background-color: #8ec43e;
    align-items: center;
    padding: 0 30px;
  }

  .header_img {
    width: 17px;
    height: 30px;
  }

  .title {
    text-align: center;
    flex: 1;
    font-size: 30px;
    color: #ffff;
  }

  .save {
    font-size: 28px;
    color: #ffffff;
  }

  .base_message {
    margin-bottom: 25px;
  }

  .inputGroup {
    border-bottom: 1px solid #efefef;
    box-sizing: border-box;
    padding: 36px 30px;
    background-color: #ffffff;
    display: flex;
    align-items: center;
    height: 98px;
  }

  .input_title {
    font-size: 28px;
    font-weight: 400;
    color: rgba(43, 43, 43, 1);
  }

  .input_value {
    text-align: right;
    flex: 1;
    font-size: 28px;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
  }

  .input_value::placeholder {
    font-size: 28px;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
  }

  .checkbox {
    position: relative;
    box-sizing: border-box;
    display: flex;
    width: 30px;
    height: 30px;
    border: 2px solid rgba(153, 153, 153, 1);
    border-radius: 5px;
  }

  .flex {
    flex: 1;
  }

  .selected_img {
    position: absolute;
    left: -2px;
    top: -2px;
    transition: all 0.1s linear;
    opacity: 0;
    height: 30px;
    width: 30px;
  }

  .selected_show {
    opacity: 1;
  }

  .work_describe {
    background-color: #ffffff;
    padding: 31px 35px 31px 30px;
  }

  .work_title {
    font-size: 28px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(43, 43, 43, 1);
  }

  .work_text {
    padding: 30px 0 0 1px;
  }

  .work_p {
    width: 100%;
    font-size: 24px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    color: rgba(153, 153, 153, 1);
    line-height: 36px;
    word-spacing: 5px;
  }

  .work_p::placeholder {
    color: rgba(153, 153, 153, 1);
  }
  .workProve{
    background-color: #ffffff;
    padding:36px 36px 29px 30px
  }
  .prove_title{
    margin-bottom: 30px;
    font-size:28px;
    font-family:PingFangSC-Regular;
    font-weight:400;
    color:rgba(43,43,43,1);
  }
  .prove_explain{
    font-size:24px;
    font-family:PingFangSC-Regular;
    font-weight:400;
    color:rgba(153,153,153,1);
    line-height:36px;
  }
  .prove_picture{
    display: flex;
    flex-flow: row wrap;
    flex-direction: row;
    justify-content: flex-start;
    align-items: stretch;
  }
  .pic{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 220px;
    height: 180px;
    background:rgba(207,207,207,1);
    margin-bottom: 23px;
  }
  .picImage{
    position: relative;
    margin-right: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 220px;
    height: 180px;
    margin-bottom: 23px;
  }
  .pic_I{
    width: 220px;
    height: 180px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .prove-add{
    width: 60px;
    height: 60px;
  }
  .deleteIcon{
    position: absolute;
    top: 0;
    right: 0;
    width: 50px;
    height: 50px;
    background: url("~@/assets/images/del_icon@2x.png") left center / 50px 50px  no-repeat;
  }
</style>
